
<div class="wrapper wrapper-content page-heading animated fadeInRight">

    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list-2">
                    <li class="links active" id="search-link"><a href="#search" role="tab" data-toggle="tab">Search</a></li>
                    <li class="links" id="search-link"><a href="#stats" role="tab" data-toggle="tab">Status</a></li>
                </ul>
                <div class="tab-content">
                   <div id="search" class="tab-pane active">
                        <div class="panel-body">
                              <form id="searchForm" class="form-horizontal">
                                  <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Search</i></h5>
                                  </div>
                                     <div class="ibox-content">
                                          <div class="form-group">
                                              <label class="col-lg-4 control-label">Name</label>
                                              <div class="col-lg-5">
                                                  <input type="text" id="name" class="form-control">
                                              </div>
                                          </div>
                                           <div class="form-group">
                                              <label class="col-lg-4 control-label">Email Address</label>
                                              <div class="col-lg-5">
                                                  <input type="text" id="email" class="form-control">
                                              </div>
                                          </div>
                                          <div class="form-group">
                                              <label class="col-lg-4 control-label"></label>
                                              <div class="col-lg-5">
                                                  <a href="javascript:document.getElementById('searchForm').reset();" class="btn btn-warning btn-md pull-right m-t-n-xs clear-btn" id="search-form"> Clear</a>
                                                  <button id="search-btn" class="btn btn-primary btn-md pull-right m-t-n-xs search-btn btn-margin-right" > Search</button>
                                              </div>
                                          </div>
                                  </form></div>
                                  <button id = "adds" class="btn btn-success m-t-n-xs btn-md add-tab-btn" type="button"> Add</button>
                                  <div class="ibox-content">
                                      <table data-filter="#filter" class="footable table table-stripped table-hover table-bordered toggle-arrow-tiny table-compact" data-page-navigation=".pagination" data-limit-navigation="5" data-page-size="50" style="width:100%;">
                                          <thead>
                                              <tr>
                                                  <th data-sort-ignore="true" style="padding:10px;text-align:center;width:120px;">Name</th>
                                                  <th data-sort-ignore="true" style="padding:10px;text-align:center;">Email</th>
                                                  <th data-sort-ignore="true" style="padding:8px;text-align:center;width:75px;"></th>
                                              </tr>
                                          </thead>
                                          <tbody id="resultfound">
                                          </tbody>
                                          <tfoot>
                                            <tr >
                                                <td colspan="4" style='border: none !important;'>
                                                    <ul class="pagination pull-right"></ul>
                                                </td>
                                            </tr>
                                        </tfoot>
                                      </table>
                              </div>
                        </div>
                    </div>
                    <div id="stats" class="tab-pane">
                        <div class="panel-body">
                        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Status</i></h5></div>
                        <table data-filter="#filter" class="footable table table-stripped table-hover table-bordered toggle-arrow-tiny table-compact" data-page-navigation=".pagination" data-limit-navigation="5" data-page-size="50">
                          <thead>
                            <tr>
                              <th data-sort-ignore="true">Name</th>
                              <th data-sort-ignore="true">Email</th>
                              <th data-sort-ignore="true">Report Name</th>
                              <th data-sort-ignore="true">Status</th>
                              <th data-sort-ignore="true">Last Sent</th>
                            </tr>
                          </thead>
                          <tbody id="result">
                            
                          </tbody>
                          <tfoot >
                              <tr >
                                  <td colspan="4" style='border: none !important;'>
                                      <ul class="pagination pull-right"></ul>
                                  </td>
                              </tr>
                          </tfoot>
                        </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- END of class row-->
</div>
<input type="hidden" id="count_add" value="0">
<input type="hidden" id="count_edit" value="0">
<input type="hidden" id="count_view" value="0">
<script>
    $(document).ready(function(){

        var interval = 1500;  // 1000 = 1 second, 3000 = 3 seconds
        function doAjax() {
          $.ajax({
                  type: 'POST',
                  url: '<?php echo base_url($this->session->userdata('forajax') . '/stats'); ?>',
                  dataType: 'json',
                  success: function (data) {
                          $('#result').html(data.result);
                  },
                  complete: function (data) {
                          setTimeout(doAjax, interval);
                  }
          });
        }
        setTimeout(doAjax, interval);

        var count_edit  = 0;
        var count_view  = 0;
        $('#adds').click(function(){
            $('#notify').text("");
                if($('#count_add').val() == 0){
                    $('#tab-list-2').append('<li id="add-link" class="links"><a id="2nd" data-toggle="tab" href="#add">Add<button id="addX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                    $('.tab-content').append('<div id="add" class="tab-pane"><div class="panel-body"><div id="change2"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                    $("#change2").load("<?php echo $this->session->userdata('forload') . '/add_form'; ?>")
                    $('#count_add').val('1');
                    add_action('show'); search_action('close');
                }
        });
        $('.close-link').click(function(){
            $('#searchResult').hide();
        });

        $(document).on('click','.open_modal',function(){
            var form_id = $(this).closest('tr').children('input[name=form_id]').val();
            var email_dtl_id = $(this).closest('tr').children('input[name=email_dtl_id]').val();
            var times = $(this).closest('tr').children('input[name=times]').val();
            var lookup_code = $(this).closest('tr').children('input[name=lookup_code]').val();
            var ena_flag = $(this).closest('tr').children('input[name=enable_flag]').val();
            $('#report_modal input[name=email_dtl_id]').val(email_dtl_id);
            $('#report_modal select[name=report_id]').val(form_id);
            $('#report_modal select[name=times]').val(times);
            $('#report_modal select[name=schedule]').val(lookup_code);
            //$('#report_modal input[name=enable_flag]').attr('checked', (ena_flag == 'Y' ? true : false));
            $('#report_modal input[name=enable_flag]').iCheck((ena_flag == 'Y' ? 'check' : 'uncheck'));
            $(".chosen").trigger('chosen:updated');
            console.log(email_dtl_id);
            console.log(lookup_code);
            console.log(times);
            console.log(ena_flag);
         });

        $('#save_report_dtl_upd').click(function(){
            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/updateReportDtl'); ?>",
                cache: false,
                dataType: 'json',
                data: $('#report_modal #form_report_dtl_upd').serialize(),
                success: function(e) {
                  console.log(e);
                },
                beforeSend: function(){
                  $("#save_report_dtl_upd-btn").attr('disabled',true);
                }
              });
        });

        $("#search-btn").click(function(e) {
            e.preventDefault();
            var name = $('#name').val();
            var email = $('#email').val();
            $('#resultfound').empty();
            $('#searchResult').show();
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/search'); ?>",
                cache: false,
                dataType: 'json',
                data: {
                    "name" : name,
                    "email" : email
                },
                success: function(e) {
                    if(e.result == null){
                        $('#resultfound').html("<td colspan='3' align='center'><h3>No Result Found</h3></td>");
                        notifymsg("danger","No Result Found");
                    }else{
                        $('#resultfound').html(e.result);
                        notifymsg("success","   . . . Search Success  , Found("+e.count+") Item(s)");
                    }
                    $('.i-checks').iCheck({
                        checkboxClass: 'icheckbox_square-green',
                        radioClass: 'iradio_square-green'
                    });
                },
                error: function() {
                },
                complete: function() {
                  $("#search-btn").removeAttr('disabled');
                },
                beforeSend: function(){
                  $("#search-btn").attr('disabled',true);
                }
              });
            });

        $(document).on('click','.edit-tab',function(){
            var moduleID = $(this).closest('tr').children('#moduleId').val();
            if($('#count_edit').val() == 0){
                $('#tab-list-2').append('<li id="edit-link" class="links"><a data-toggle="tab" href="#edit">Edit<button id="editX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="edit" class="tab-pane"><div class="panel-body"><div id="change3"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                $("#count_edit").val('1');
                }
            $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+moduleID);
            edit_action('show'); search_action('close');
            notifymsg("hide","hide");
            });
        $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.view',function(){
            var moduleID = $(this).closest('tr').children('#moduleId').val();
            if($('#count_view').val() == 0){
                $('#tab-list-2').append('<li id="view-link" class="links"><a data-toggle="tab" href="#view">View<button id="viewX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="view" class="tab-pane"><div class="panel-body"><div id="change4"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                $("#count_view").val('1');
                }
            $("#change4").load("<?php echo $this->session->userdata('forload') . '/view_form/'; ?>"+moduleID);
            view_action('show'); search_action('close');
            notifymsg("hide","hide");
            });
        });
</script>